package com.sc.highspeed.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

/**
 * Created by Admin on 2017/12/9.
 */
@Component
@Slf4j
public class WebSocketInterceptor implements HandshakeInterceptor {
    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest,
                                   ServerHttpResponse serverHttpResponse,
                                   WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
        String clientId = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getParameter("clientId");
        if (clientId != null) {
            map.put("clientId",clientId);
//            log.info("HandShaking");
            System.out.println("HandShaking");
        } else {
//            log.error("ClientId is Null");
            System.out.println("ClientId is Null");
            return false;
        }
        return true;
    }

    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {
        System.out.println("After Handshake");
//        log.info("After Handshake");
    }
}
